package org.example.main;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Bus extends Activity implements OnClickListener {

	TableLayout righeTabellaBus;
	View bottoneSinistroMenuBus;
	View bottoneCentraleMenuBus;
	View bottoneDestroMenuBus;
	String percorso0 = "Via Andrea Doria, Via Duca degli Abruzzi, Viale Vittorio Veneto, Corso Italia";
	String percorso1 = "Via Passo Gravina, via Filocomo, Piazza Cavour, via Rocca Romana, viale Regina Margherita, Viale XX settembre";
	String percorso2 = "Via Andrea Doria, via Etnea, Viale XX settembre";
	String percorso3 = "Via Andrea Doria, via Odorico da Pordenone, via Vincenzo Giuffrida, Corso delle province";
	int [] tempiAttesaBus = {6, 12, 23, 37};

	String [] percorsi = {percorso0, percorso1, percorso2, percorso3};
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bus);
		righeTabellaBus = (TableLayout)findViewById(R.id.righeTabellaBus);
		bottoneSinistroMenuBus = this.findViewById(R.id.bottoneSinistroMenuBus);
		bottoneSinistroMenuBus.setOnClickListener(this);
		bottoneCentraleMenuBus = this.findViewById(R.id.bottoneCentraleMenuBus);
		bottoneCentraleMenuBus.setOnClickListener(this);
		bottoneDestroMenuBus = this.findViewById(R.id.bottoneDestroMenuBus);
		bottoneDestroMenuBus.setOnClickListener(this);
		((ImageView)this.findViewById(R.id.immagineBottoneSinistroBus)).setBackgroundDrawable(this.getResources().getDrawable(android.R.drawable.ic_menu_revert));
		((ImageView)this.findViewById(R.id.immagineBottoneCentraleBus)).setBackgroundDrawable(this.getResources().getDrawable(R.drawable.ic_menu_chat_dashboard));
		((ImageView)this.findViewById(R.id.immagineBottoneDestroBus)).setBackgroundDrawable(this.getResources().getDrawable(android.R.drawable.ic_menu_set_as));
		inizializzaTabellaBus(righeTabellaBus);
	}

	public void inizializzaTabellaBus(TableLayout tabella)
		{ 
		TableRow riga;
		for (int i=0; i<percorsi.length; i++)
			{
			riga = new TableRow(this);
			riga.setMinimumHeight(35);
			riga.setGravity(Gravity.CENTER);

			if (i%2 == 0)
				riga.setBackgroundColor(getResources().getColor(R.color.grigioRigheTabella));
			else
				riga.setBackgroundColor(getResources().getColor(R.color.bianco));

			TextView numeroLinea = new TextView(this);
			int linea = (int)((Math.random()*1000000%900))+100;
			numeroLinea.setText(Integer.toString(linea));
			numeroLinea.setTextColor(getResources().getColor(R.color.grigioScurissimo));
			numeroLinea.setTypeface(Typeface.DEFAULT_BOLD);
			numeroLinea.setGravity(Gravity.CENTER);
			numeroLinea.setPadding(5, 0, 5, 0);

			TextView tempoAttesaBus = new TextView(this);
			//int attesa = (int)((Math.random()*1000000%21));
			int attesa = tempiAttesaBus[i];
			tempoAttesaBus.setText(attesa + "'");
			tempoAttesaBus.setTextColor(getResources().getColor(R.color.grigioScurissimo));
			tempoAttesaBus.setTypeface(Typeface.DEFAULT_BOLD);
			tempoAttesaBus.setGravity(Gravity.CENTER);

			TextView tempoArrivo = new TextView(this);
			int tempoPercorrenza = (int)((Math.random()*1000000%31))+21;
			int arrivo = attesa + tempoPercorrenza;
			int oreTempoArrivo = arrivo/60;
			int minutiTempoArrivo = arrivo%60;
			tempoArrivo.setText(oreTempoArrivo + "h " + minutiTempoArrivo + "'");
			tempoArrivo.setTextColor(getResources().getColor(R.color.grigioScurissimo));
			tempoArrivo.setTypeface(Typeface.DEFAULT_BOLD);
			tempoArrivo.setGravity(Gravity.CENTER);
			
			LinearLayout infoTragittoConIconaAccordion = new LinearLayout(this);
			infoTragittoConIconaAccordion.setOrientation(LinearLayout.HORIZONTAL);
			infoTragittoConIconaAccordion.setGravity(Gravity.CENTER);
			infoTragittoConIconaAccordion.setPadding(5, 0, 5, 0);

				ImageView infoTragitto = new ImageView(this);
				infoTragitto.setPadding(5, 0, 5, 0);
				int personeABordo = (int)(Math.random()*1000000)%100;
				if (personeABordo<30)
					infoTragitto.setImageResource(R.drawable.discopiccolissimoverde);
				else
					if (personeABordo>70)
						infoTragitto.setImageResource(R.drawable.discopiccolissimorosso);
					else
						infoTragitto.setImageResource(R.drawable.discopiccolissimogiallo);
	
				ImageView iconaAccordion = new ImageView(this);
				iconaAccordion.setImageDrawable(getResources().getDrawable(R.drawable.accordionchiusopiccolo));
				iconaAccordion.setPadding(5, 0, 5, 0);

			infoTragittoConIconaAccordion.addView(infoTragitto);
			infoTragittoConIconaAccordion.addView(iconaAccordion);
			
			riga.addView(numeroLinea);
			riga.addView(tempoAttesaBus);
			riga.addView(tempoArrivo);
			riga.addView(infoTragittoConIconaAccordion);
			riga.setId(i*2);
			riga.setOnClickListener(this);
			tabella.addView(riga);
			
			LinearLayout containerInfoLinea = new LinearLayout(this);
			containerInfoLinea.setId(i*2+1);
			containerInfoLinea.setBackgroundColor(getResources().getColor(R.color.primaRigaTabellaMeteo));
			containerInfoLinea.setPadding(0, 0, 0, 1);
			containerInfoLinea.setVisibility(View.GONE);
			
			TextView infoLinea = new TextView(this);
			infoLinea.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			infoLinea.setText(percorsi[i]);
			infoLinea.setPadding(5, 2, 5, 2);
			//infoLinea.setBackgroundColor(getResources().getColor(R.color.percorsoLineaAutobusScelta));
			infoLinea.setBackgroundColor(getResources().getColor(R.color.bianco));
			infoLinea.setTextColor(getResources().getColor(R.color.nero));

			containerInfoLinea.addView(infoLinea);
			tabella.addView(containerInfoLinea);
			
			}
		}

	@Override
	public void onClick(View v)
		{
		switch(v.getId())
			{
			case R.id.bottoneSinistroMenuBus:
				bottoneSinistroMenuBus.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.timepickerinputselected));
		        finish();
				break;
			case R.id.bottoneCentraleMenuBus:
	        	Intent home = new Intent(this, CentroCommerciale.class);
				bottoneCentraleMenuBus.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.timepickerinputselected));
				startActivity(home);
				break;
			case R.id.bottoneDestroMenuBus:
				//Intent uscita = new Intent(this,SchermataAvvio.class); 
				bottoneDestroMenuBus.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.timepickerinputselected));
				//startActivity(uscita);
				finish();
				break;
			default:
				visualizzaPercorso(v.getId());
				break;
			}
		}

	public void visualizzaPercorso(int id)
		{
		TableRow rigaCliccata = (TableRow)righeTabellaBus.findViewById(id);
		LinearLayout containerInfoLineaScelta = (LinearLayout)findViewById(id+1);
		ImageView iconaAccordionCliccato = (ImageView)((LinearLayout)rigaCliccata.getChildAt(3)).getChildAt(1);
		TableRow rigaTabella;
		LinearLayout containerInfoLinea;
		if (containerInfoLineaScelta.getVisibility()==View.VISIBLE)
			{
			containerInfoLineaScelta.setVisibility(View.GONE);
			iconaAccordionCliccato.setImageDrawable(getResources().getDrawable(R.drawable.accordionchiusopiccolo));
			}
		else
			{
			for (int i=0; i<righeTabellaBus.getChildCount()-1; i+=2)
				{
				rigaTabella = (TableRow)(righeTabellaBus.findViewById(i));
				containerInfoLinea = (LinearLayout)(righeTabellaBus.findViewById(i+1));
				containerInfoLinea.setVisibility(View.GONE);
				ImageView iconaAccordion = (ImageView)((LinearLayout)rigaTabella.getChildAt(3)).getChildAt(1);
				iconaAccordion.setImageDrawable(getResources().getDrawable(R.drawable.accordionchiusopiccolo));
				if (i%4 == 0)
					rigaTabella.setBackgroundColor(getResources().getColor(R.color.grigioChiaro));
				else
					rigaTabella.setBackgroundColor(getResources().getColor(R.color.bianco));
				}
			rigaCliccata.setBackgroundResource(R.color.primaRigaTabellaMeteo);
			iconaAccordionCliccato.setImageDrawable(getResources().getDrawable(R.drawable.accordionapertopiccolo));
	
			Animation animazioneApertura;
			animazioneApertura = AnimationUtils.makeInChildBottomAnimation(this);
			containerInfoLineaScelta.setVisibility(View.VISIBLE);
			containerInfoLineaScelta.startAnimation(animazioneApertura);
			}
		
		
		}

	public void onPause()
		{
		super.onPause();
		/* Ripristino l'aspetto dei bottoni del menu riportandoli a quello di default */
		bottoneSinistroMenuBus.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.timepickerinputnormal));
		bottoneCentraleMenuBus.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.timepickerinputnormal));
		bottoneDestroMenuBus.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.timepickerinputnormal));
		}

}
